﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace RestService
{
    public class RWFile
    {
        public static void ExportToTextFile(double[,] maTran, int soHang, int soCot, string fileName)
        {
            //string temp = "";
            //Điền thông tin hàng 0 và cột 0 là các ID
            for (int i = 0; i < soHang; i++)
            {
                maTran[i, 0] = i;
            }
            for (int i = 0; i < soCot; i++)
            {
                maTran[0, i] = i;
            }

            //
            string path = string.Format(@"C:\\{0}.txt", fileName);
            using (StreamWriter writer = new StreamWriter(path, false))
            {
                for (int i = 0; i < soHang; i++)
                {
                    //temp = "";
                    for (int j = 0; j < soCot; j++)
                    {
                        writer.Write(Math.Round(maTran[i, j], 2).ToString() + "\t");
                    }
                    //Ghi ra file
                    writer.WriteLine();
                }
            }
        }

        public static void ExportToTextFile(string[] str, string fileName)
        {
            string path = string.Format(@"C:\\{0}.txt", fileName);
            using (StreamWriter writer = new StreamWriter(path, false))
            {
                for (int j = 0; j < str.Length; j++)
                {
                    writer.Write(str[j] + "\n");
                }
                //Ghi ra file
                writer.WriteLine();
            }
        }

        public static double[,] ReadFromTextFile(string fileName)
        {
            double[,] maTran;
            string path = string.Format(@"C:\\{0}.txt", fileName);
            string[] lines = System.IO.File.ReadAllLines(path);

            int soHang = lines.Length;
            int soCot = lines[0].Split('\t').Length;
            maTran = new double[soHang, soCot];

            for (int i = 0; i < soHang; i++)   //Hàng
            {
                string[] line = lines[i].Split('\t');
                for (int j = 0; j < soCot; j++) //Cột
                {
                    maTran[i, j] = double.Parse(line[j]);
                }
            }
            return maTran;
        }
    }
}
